package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.web.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.query.HotelQueryObjext;
import cn.wolfcode.trip.base.service.IHotelService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/hotel")
public class HotelController {

    @Autowired
    private IHotelService service;
    @Autowired
    private IRegionService regionService;


    //查询所有的酒店,用于高级查询
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") HotelQueryObjext qo, Model model) {
        qo.setPageSize(5);
        PageInfo info = service.query(qo);
        model.addAttribute("result", info);
        model.addAttribute("places", regionService.list());
        return "/hotel/list";
    }

    //查询单个酒店,进行回显
    @RequestMapping("/{id}")
    public Object get(@PathVariable Long id) {
        return service.get(id);
    }


    //游记推荐的新增和编辑操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Hotel hotel, MultipartFile pic) {

        //封面图片需要上传 然后设置进功游记攻略对象中
        if (pic != null && pic.getSize() > 0) {
            String imagePath = UploadUtil.upload(pic, UploadUtil.IMG_PATH + "upload");
            hotel.setCoverUrl(imagePath);
        }
        if (hotel.getId() == null) {
            service.save(hotel);
        } else {
            service.update(hotel);
        }
        return new JSONResult();
    }


}
